/** 
 * Send an Ajax request
 *
 * @param action action name
 * @param data object data
 * @param func callback function
 * @param secure security mode
 * @example send('getwidget', {key1:'value1', key2:'value2'}, function(result) { your code }, false);
 * @default secure = true
 */
function send (action, data, func, secure)
{
    secure = secure || false;
    //var syn = app.root+'/syn/';
    var syn = app.root+'syn/';
    if (secure) {
        data.uid = app.uid;
        data.token = app.token;
    }
    var url = syn+action.toLowerCase();
    $.ajax({
        url: url,
        type: 'POST',
        async: false,
        data:data
    }).success(function(result) {
        func(result);
    });
};

/**
 * Render HTML template
 *
 * @param template id of element
 * @param data object
 * @example <div class="template" id="abc"><div class="{key1}">{key2}</div></div>
 * @example render('abc', {key1:'name', key2:'Hello My name is Loi'});
 */
function render(template, data)
{
    var html = $('#'+template).html();
    var pattern = /\{[\sa-z0-9_]+\}/igm;
    var result = '';
    var index = 0;
    var var_;

    while (match = pattern.exec(html)) {
        var_ = match[0].substring(1,match[0].length-1).trim();
        result+=html.substring(index, match.index);
        eval('result+=data.'+var_);
        index = pattern.lastIndex;
    }
    
    result+=html.substring(index, html.length);
    return result;
}